package com.campus.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
 * @desc:
 * @author: Tournesol234
 * @date: 2024/11/22 14.41
 */
public class AuthorizationUtils {
    public static boolean hasPermission(String username, String permission) {
        try (Connection connection = JDBCUtils.getConnection()) {
            String sql = "SELECT p.code FROM t_sys_permission p " +
                    "JOIN t_sys_role_permission rp ON p.id = rp.perm_id " +
                    "JOIN t_sys_user_role ur ON rp.role_id = ur.role_id " +
                    "JOIN users u ON ur.user_id = u.id " +
                    "WHERE u.login_name = ? AND p.code = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, permission);
            ResultSet resultSet = statement.executeQuery();
            return resultSet.next();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
